﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using NetBay.Core.BusinessEntities;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NetBay.Core.DataAccess;
using NetBay.Core.Log;
using Rio.Doc.Business.Repository.Translator;
using Rio.Doc.DataAccess;
#endregion

namespace Rio.Doc.Business.Repository
{
    /// <summary>
    /// Organization Repository
    /// </summary>
    public class OrganizationRepository : OrganizationDer
    {
        public override void SetFolderTypeList(long p_lUserId)
        {
            FolderTypesList = FolderTypeRepository.GetFolderTypeByOrganization(p_lUserId, Identifier);
        }

        /// <summary>
        /// Gets all Rio Organization.
        /// </summary>
        /// <returns></returns>
        public static List<Organization> GetAllRioOrganization()
        {
            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("OrganizationRepository.GetAllRioOrganization (No parameters)...");

            //Variable initialization.
            var l_oReturnLstRioOrganization = new List<Organization>();

            try
            {
                //Get all Rio Organization.
                l_oReturnLstRioOrganization = OrganizationTranslator.TranslateOrganization(DA_Organization.GetAllOrganization());
            }

            catch (Exception ex)
            {
                //Error log.
                NetBay.Core.Log.LogManager.Instance().LogError("OrganizationRepository.GetAllRioOrganization Exception [" + ex.Message + "]");

                //Return null.
                l_oReturnLstRioOrganization = null;
            }

            //Return value.
            return l_oReturnLstRioOrganization;
        }


        /// <summary>
        /// Updates the rank organization.
        /// </summary>
        /// <param name="orgaSourceId">The orga source id.</param>
        /// <param name="orgaSourceRank">The orga source rank.</param>
        /// <param name="orgaDestinationId">The orga destination id.</param>
        /// <param name="orgaDestinationRank">The orga destination rank.</param>
        /// <returns></returns>
        public static bool UpdateRankOrganization(long orgaSourceId, int orgaSourceRank, long orgaDestinationId, int orgaDestinationRank)
        {
            return DA_Organization.UpdateRankOrganization(orgaSourceId,
                                                          orgaSourceRank,
                                                          orgaDestinationId,
                                                          orgaDestinationRank);
        }

        /// <summary>
        /// Gets the organizations by user id.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <returns></returns>
        public static List<Organization> GetOrganizationsByUserId(long userId)
        {
            //Log.
            LogManager.Instance().LogDebug("OrganizationRepository.GetOrganizationsByUserId (userId <" + userId + ">)...");

            //Variable initialization.
            var organizations = new List<Organization>();

            try
            {
                //Get all Rio Organization.
                organizations = OrganizationTranslator.TranslateOrganization(DA_Organization.GetOrganizationsByUserId(userId));
            }

            catch (Exception ex)
            {
                //Error log.
                NetBay.Core.Log.LogManager.Instance().LogError("OrganizationRepository.GetOrganizationsByUserId Exception [" + ex.Message + "]");

                //Return null.
                organizations = null;
            }

            //Return value.
            return organizations;
        }


        /// <summary>
        /// Gets the organization id.
        /// </summary>
        /// <param name="organizationId">The organization id.</param>
        /// <returns></returns>
        public static Organization GetOrganizationId(long organizationId)
        {
            //Log.
            LogManager.Instance().LogDebug("OrganizationRepository.GetOrganizationsByUserId (organizationId <" + organizationId +
                                           ">)...");

            //Variable initialization.
            var organization = new Organization();

            try
            {
                //Get all Rio Organization.
                organization =
                    OrganizationTranslator.TranslateOneOrganization(DA_Organization.GetOrganizationId(organizationId));
            }

            catch (Exception ex)
            {
                //Error log.
                NetBay.Core.Log.LogManager.Instance().LogError(
                    "OrganizationRepository.GetOrganizationId Exception [" + ex.Message + "]");

                //Return null.
                organization = null;
            }

            //Return value.
            return organization;
        }

        /// <summary>
        /// Gets the organizations by user id.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <returns></returns>
        public static List<Organization> GetDisctinctOrganizationsByUserId(long userId)
        {
            return
                OrganizationTranslator.TranslateOrganization(DA_Organization.GetDisctinctOrganizationsByUserId(userId));
        }

        /// <summary>
        /// Deletes a list of organizations.
        /// </summary>
        /// <param name="identifiers">The identifiers.</param>
        /// <returns></returns>
        public static StoredProcedureResultCollection DeleteOrganizations(List<long> identifiers)
        {
            StoredProcedureResultCollection result = new StoredProcedureResultCollection();
            foreach (long identifier in identifiers)
            {
                result.Add(DA_Organization.DeleteOrganization(identifier));
            }
            return result;
        }

        /// <summary>
        /// Deletes one organization.
        /// </summary>
        /// <param name="identifier">The identifier.</param>
        /// <returns></returns>
        public static StoredProcedureResult DeleteOrganization(long identifier)
        {
            return DA_Organization.DeleteOrganization(identifier);
        }

        /// <summary>
        /// Adds one organization.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="description">The description.</param>
        /// <returns></returns>
        public static StoredProcedureResult AddOrganization(string name, string description)
        {
            return DA_Organization.AddOrganization(name, description);
        }

        /// <summary>
        /// Updates the organization.
        /// </summary>
        /// <param name="identifier">The identifier.</param>
        /// <param name="name">The name.</param>
        /// <param name="description">The description.</param>
        /// <param name="rank">The rank.</param>
        /// <param name="isdeleted">if set to <c>true</c> [isdeleted].</param>
        /// <returns></returns>
        public static StoredProcedureResult UpdateOrganization(long identifier, string name, string description, bool isdeleted = false)
        {
            return DA_Organization.UpdateOrganization(identifier, name, description, isdeleted);
        }
    }        
}
